"""
用例执行和生成测试报告
"""
import os
import unittest
from htmltestreport import HTMLTestReport
from config import DIR_PATH


script_path=DIR_PATH+os.sep+"script"
suite = unittest.defaultTestLoader.discover(script_path)
file_path = DIR_PATH + os.sep + "report" + os.sep + "webshop1.html"
HTMLTestReport(file_path,"tpshop商城测试报告").run(suite)



import smtplib
# 处理邮件附件，需要导入MIMEMultipart,Header,MIMEBase
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.base import MIMEBase
from email import encoders  # 编码格式


# # 邮箱属性配置
mailserver = 'smtp.163.com'   # # 邮箱服务端
# 发件人-此邮箱是我随便写的
userName_SendMail = 'spy1378010843@163.com'
# 邮箱发件授权码-为发件人邮箱根据第四步生成的授权码
userName_AuthCode = 'MHRUYIJTWKFHPGVC'
# 定义邮件的接收者-我随便写的，若收件人较多，可用列表表示
received_mail = ['spy1378010843@163.com']

# 邮件中发送附件
# 附件配置邮箱
email = MIMEMultipart()
email['Subject'] = 'tpshop测试报告'  # 定义邮件主题
email['From'] = userName_SendMail  # 发件人
email['To'] = ','.join(received_mail)  # 收件人（可以添加多个，若只有一个收件人，可直接写邮箱号）

# 附件处理
att = MIMEBase('application', 'octet-stream')  # 规范

att.set_payload(open(file_path, 'rb').read())  # 读取测试报告
att.add_header('Content-Disposition', 'attachment', filename=Header('tpshop.html', 'gbk').encode())
encoders.encode_base64(att)
email.attach(att)

# 发送邮件
smtp = smtplib.SMTP_SSL(mailserver, port=465)  # 邮箱的端口号是465或587不能用25，其他邮箱的端口号可自行百度，非QQ邮箱，一般使用SMTP即可，不需要有SSL
smtp.login(userName_SendMail, userName_AuthCode)
smtp.sendmail(userName_SendMail, ','.join(received_mail), email.as_string())
smtp.quit()




